#!/bin/sh
# ----------------------------------------------------------------------
# Copyright © 2011, libcork authors
# All rights reserved.
#
# Please see the COPYING file in this distribution for license details.
# ----------------------------------------------------------------------

# Usage:
#   calculate version|commit [top_srcdir] [path to stamp file]
#
# Calculates the current version number.  When run from a distribution tarball,
# we get the version number from the .version-stamp file (that our `make dist`
# target ensures that it creates).  When run from a local git repository, we get
# the version number via `git describe`.

set -e

WHAT="$1"
case "$WHAT" in
  version) ;;
  commit) ;;
  *) echo "Unknown option $WHAT" >&2; exit 1;;
esac

top_srcdir="${2-.}"
export GIT_DIR="${top_srcdir}/.git"

# First try the stamp file
STAMP_FILE="$3"
if [ -f "$STAMP_FILE" ]; then
  version=$(cat "$STAMP_FILE")
  if [ -z "$version" ]; then
    echo "Invalid stamp file" >&2
    exit 1
  fi
  printf "%s" "$version"
  exit 0
fi

# Fall back on `git describe`
case "$WHAT" in
  version)
    closest_tag=$(git describe --abbrev=0)
    full_version=$(git describe --abbrev=7 --dirty)
    if test "$closest_tag" = "$full_version"; then
        version="$closest_tag"
    else
        version="$closest_tag-git"
    fi
    ;;
  commit)
    version=$(git rev-parse HEAD);;
esac
if [ -z "$version" ]; then
  echo "Cannot find the version from git" >&2
  exit 1
fi
printf "%s" "$version"
